﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VNPT;
using VNPT.Data;

namespace VNPT.DoiSoat
{
    public partial class uCapNhatDVBB_MaMang : Core.uBase
    {
        bool Create_810 = true;
        bool Create_TanLoi = true;
        bool Create_DVBB = true;

        bool Drop_810 = false;
        bool Drop_TanLoi = false;


        public uCapNhatDVBB_MaMang()
        {
            InitializeComponent();
        }

        public override void LoadSetting()
        {
            chk_810.Text = string.Format("CT_HOST_810_{0}_{1:D2}a_DLK", DoiSoatSetting.NAM, DoiSoatSetting.THANG);
            chk_TanLoi.Text = string.Format("CT_HOST_TANLOI_{0}_{1:D2}a_DLK", DoiSoatSetting.NAM, DoiSoatSetting.THANG);
            chk_DVBB.Text = string.Format("CT_DVBB_ALL_{0}_{1:D2}", DoiSoatSetting.NAM, DoiSoatSetting.THANG);

            chk_Drop_810.Text = string.Format("CT_HOST_810_{0}_{1:D2}_DLK", DoiSoatSetting.NAM, DoiSoatSetting.THANG);
            chk_Drop_TanLoi.Text = string.Format("CT_HOST_TANLOI_{0}_{1:D2}_DLK", DoiSoatSetting.NAM, DoiSoatSetting.THANG);
            
            base.LoadSetting();
        }
        public override void SaveSetting()
        {
            base.SaveSetting();
        }

        string strCnn = VNPT.Data.DataSetting.CONNECTSTRING_TNS_NAME;
       
        private void Create_Table_TanLoi(string TableName, string TableGoc)
        {

                txtTienTrinh = string.Format("Tạo bảng {0}....Có", TableName);
                bgWork.ReportProgress(10);

                txtTienTrinh = string.Format("Đang tạo bảng {0} .... .......", TableName);
                bgWork.ReportProgress(15);

                if (bgWork.CancellationPending) return;

                var sql1 = string.Format(@"CREATE TABLE {0} AS
                            Select CHUGOI, BIGOI, NGAYBD, GIOBD, PHUT, TGGOI, INC_ROUTE, OUT_ROUTE,
                            DOISOAT.GETDVBB(BIGOI) DVBB, DOISOAT.GETMAMANG(CHUGOI) MA_MANG, DOISOAT.GETHOPTIN810X(BIGOI) HOPTIN
                            From {1}", TableName, TableGoc);

                DbServer.ExecuteQuery(new CommandConfig(sql1));
                txtTienTrinh = string.Format("Đã tạo thành công bảng {0} .... .......", TableName);
                bgWork.ReportProgress(80);

                var sql2 = string.Format("create index {0} on {0} (CHUGOI)", TableName);
                bgWork.ReportProgress(85);

                txtTienTrinh = string.Format("Đang tạo INDEX cho bảng {0} .... .......", TableName);
                bgWork.ReportProgress(90);

                if (bgWork.CancellationPending) return;

                DbServer.ExecuteQuery(new CommandConfig(sql2));

                txtTienTrinh = string.Format("Đã tạo thành công ....");
                bgWork.ReportProgress(100);

        }
       
        private void Create_Table_DVBB(string TableName, string TableGoc)
        {

                txtTienTrinh = string.Format("Tạo bảng {0}....Có", TableName);
                bgWork.ReportProgress(10);

                txtTienTrinh = string.Format("Đang tạo bảng {0} .... .......", TableName);
                bgWork.ReportProgress(15);

                if (bgWork.CancellationPending) return;

                var sql1 = string.Format(@"CREATE TABLE {0} AS
                           Select *  From {1} Where DVBB is not null", TableName, TableGoc);

                DbServer.ExecuteQuery(new CommandConfig(sql1));
                txtTienTrinh = string.Format("Đã tạo thành công bảng {0} .... .......", TableName);
                bgWork.ReportProgress(80);

                var sql2 = string.Format("create index {0} on {0} (CHUGOI)", TableName);
                bgWork.ReportProgress(85);

                txtTienTrinh = string.Format("Đang tạo INDEX cho bảng {0} .... .......", TableName);
                bgWork.ReportProgress(90);

                if (bgWork.CancellationPending) return;

                DbServer.ExecuteQuery(new CommandConfig(sql2));

                txtTienTrinh = string.Format("Đã tạo thành công ....");
                bgWork.ReportProgress(100);

        }

        private void Drop_Table(string TableName)
        {


                txtTienTrinh = string.Format("Drop bảng {0} .... ....... Có", TableName);
                bgWork.ReportProgress(10);

                txtTienTrinh = string.Format("Đang Drop bảng {0} .... .......", TableName);
                bgWork.ReportProgress(15);

                var sql1 = string.Format(@"DROP TABLE {0};", TableName);

                if (bgWork.CancellationPending)return;

                DbServer.ExecuteQuery(new CommandConfig(sql1));
                txtTienTrinh = string.Format("Đã drop thành công bảng {0} .... .......", TableName);
                bgWork.ReportProgress(100);

        }


        private void ThucThi()
        {
            if (bgWork.CancellationPending) return;
            if (Create_810)
            {
                bgWork.ReportProgress(0);
                Create_Table_TanLoi(chk_810.Text, chk_Drop_810.Text);
            }
            else
            {
                txtTienTrinh = string.Format("Tạo table {0}.... Không", chk_810.Text);
                bgWork.ReportProgress(100);
            }

            if (bgWork.CancellationPending) return;
            if (Create_TanLoi)
            {
                bgWork.ReportProgress(0);
                Create_Table_TanLoi(chk_TanLoi.Text, chk_Drop_TanLoi.Text);
            }
            else
            {
                txtTienTrinh = string.Format("Tạo table {0}.... Không", chk_TanLoi.Text);
                bgWork.ReportProgress(100);
            }


            if (bgWork.CancellationPending) return;
            if (Create_DVBB && !DbServer.TableExists(chk_DVBB.Text))
            {
                bgWork.ReportProgress(0);
                Create_Table_DVBB(chk_DVBB.Text, chk_810.Text);
            }
            else
            {
                txtTienTrinh = string.Format("Tạo bảng {0}.... Không", chk_DVBB.Text);
                bgWork.ReportProgress(100);
            }

            if (bgWork.CancellationPending) return;
            if (Drop_810)
            {
                bgWork.ReportProgress(0);
                Drop_Table(chk_Drop_810.Text);
            }
            else
            {
                txtTienTrinh = string.Format("Drop bảng {0}.... Không", chk_Drop_810.Text);
                bgWork.ReportProgress(100);
            }

            if (bgWork.CancellationPending) return;
            if (Drop_TanLoi)
            {
                bgWork.ReportProgress(0);
                Drop_Table(chk_Drop_TanLoi.Text);
            }
            else
            {
                txtTienTrinh = string.Format("Drop bảng {0}.... Không", chk_Drop_TanLoi.Text);
                bgWork.ReportProgress(100);
            }
        }
        string txtTienTrinh = "";
        private void chk_Drop_810_CheckStateChanged(object sender, EventArgs e)
        {

        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            Create_810 = chk_810.Checked;
            Create_TanLoi = chk_TanLoi.Checked;
            Create_DVBB = chk_DVBB.Checked;

            Drop_810 = chk_Drop_810.Checked;
            Drop_TanLoi = chk_Drop_TanLoi.Checked;

            string st = "\n";
            st += chk_810.Checked ? "\nCreate: " + chk_810.Text : "";
            st += chk_TanLoi.Checked ? "\nCreate: " + chk_TanLoi.Text : "";
            st += chk_DVBB.Checked ? "\nCreate: " + chk_DVBB.Text : "";
            st += "\n\n";
            st += chk_Drop_810.Checked ? "\nDrop: " + chk_Drop_810.Text : "";
            st += chk_Drop_TanLoi.Checked ? "\nDrop: " + chk_Drop_TanLoi.Text : "";
            st += "\n\n";
            if (st != "")
            {
                if (DevExpress.XtraEditors.XtraMessageBox.Show("Các thao tác thực hiện bao gồm:" + st + "Bạn có chắc chắn không?", "Xác nhận?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    TruocKhiChay();
                    bgWork.RunWorkerAsync();
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Chọn một thao tác để thực hiện!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void bgWork_DoWork(object sender, DoWorkEventArgs e)
        {
            ThucThi();
        }

        private void bgWork_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
           progressBarControl1.EditValue = e.ProgressPercentage;
           AppendTextBox(txtTienTrinh);
           lblHienThi.Text = txtTienTrinh;
        }

        private void bgWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error!=null)
            {
                AppendTextBox(e.Error.Message);
                DevExpress.XtraEditors.XtraMessageBox.Show("Hoàn thành, có phát sinh lỗi.\nVui lòng kiểm tra lại!\n"+e.Error.Message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Đã thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                AppendTextBox("Hoàn thành!");
            }
            SauKhiChay();
        }

        private void TruocKhiChay()
        {
            DevExpress.XtraEditors.XtraForm f;
            if (this.Parent == null) return;
            if (this.Parent.Parent == null) return;
            else
            {
                var t2 = this.Parent.Parent as DevExpress.XtraEditors.XtraForm;
                f = t2;
            }

            var t = this.Parent.Parent.Parent as DevExpress.XtraEditors.XtraForm;
            if (t != null)
            {
                f = t;
            }
            if (f != null)
            {
                f.FormClosing += (a1, e1) =>
                {
                    if (bgWork.IsBusy)
                    {
                        string text = "Chương trình đang chạy, không thể đóng form! Vui lòng hủy trước khi đóng chương trình?";
                        string caption = "Xác nhận?";
                        DevExpress.XtraEditors.XtraMessageBox.Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        e1.Cancel = true;
                    }
                };

            }

            var m = this.Parent as DevExpress.XtraWizard.WizardPage;
            if (m != null)
            {
                m.AllowNext = false;
                m.AllowBack = false;
            }
        }
       
        private void SauKhiChay()
        {
            var m = this.Parent as DevExpress.XtraWizard.WizardPage;
            if (m != null)
            {
                m.AllowNext = true;
                m.AllowBack = true;
            }
        }

        public void AppendTextBox(string value)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
                return;
            }
            memoEdit1.Text = string.Format("> {0:HH:mm:ss} ",DateTime.Now)+  value + Environment.NewLine + memoEdit1.Text;
        }
    }
}
